Merge branch 'data_output_agent_limits_events_after_ordering'

Akinori MUSHA 7 years ago
parent
commit
12cecb8392
2 changed files with 44 additions and 3 deletions
  1. 3 2
      app/models/agents/data_output_agent.rb
  2. 41 1
      spec/models/agents/data_output_agent_spec.rb

+ 3 - 2
app/models/agents/data_output_agent.rb

@@ -191,6 +191,8 @@ module Agents
191 191
     end
192 192
 
193 193
     def latest_events(reload = false)
194
+      received_events = received_events().reorder(id: :asc)
195
+
194 196
       events =
195 197
         if (event_ids = memory[:event_ids]) &&
196 198
            memory[:events_order] == events_order &&
@@ -208,8 +210,7 @@ module Agents
208 210
 
209 211
         new_events =
210 212
           if last_event_id = memory[:last_event_id]
211
-            received_events.where(Event.arel_table[:id].gt(last_event_id)).
212
-              order(id: :asc).to_a
213
+            received_events.where(Event.arel_table[:id].gt(last_event_id)).to_a
213 214
           else
214 215
             source_ids.flat_map { |source_id|
215 216
               # dig twice as many events as the number of

+ 41 - 1
spec/models/agents/data_output_agent_spec.rb

@@ -242,13 +242,53 @@ describe Agents::DataOutputAgent do
242 242
         })
243 243
       end
244 244
 
245
+      context 'with more events' do
246
+        let!(:event4) do
247
+          agents(:bob_website_agent).create_event payload: {
248
+            'site_title' => 'XKCD',
249
+            'url' => 'http://imgs.xkcd.com/comics/comic1.png',
250
+            'title' => 'Comic 1',
251
+            'date' => '',
252
+            'hovertext' => 'Hovertext for Comic 1'
253
+          }
254
+        end
255
+
256
+        let!(:event5) do
257
+          agents(:bob_website_agent).create_event payload: {
258
+            'site_title' => 'XKCD',
259
+            'url' => 'http://imgs.xkcd.com/comics/comic2.png',
260
+            'title' => 'Comic 2',
261
+            'date' => '',
262
+            'hovertext' => 'Hovertext for Comic 2'
263
+          }
264
+        end
265
+
266
+        let!(:event6) do
267
+          agents(:bob_website_agent).create_event payload: {
268
+            'site_title' => 'XKCD',
269
+            'url' => 'http://imgs.xkcd.com/comics/comic3.png',
270
+            'title' => 'Comic 3',
271
+            'date' => '',
272
+            'hovertext' => 'Hovertext for Comic 3'
273
+          }
274
+        end
275
+
276
+        describe 'limiting' do
277
+          it 'can select the last `events_to_show` events' do
278
+            agent.options['events_to_show'] = 2
279
+            content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
280
+            expect(content['items'].map {|i| i["title"] }).to eq(["Comic 3", "Comic 2"])
281
+          end
282
+        end
283
+      end
284
+
245 285
       describe 'ordering' do
246 286
         before do
247 287
           agent.options['events_order'] = ['{{hovertext}}']
248 288
           agent.options['events_list_order'] = ['{{title}}']
249 289
         end
250 290
 
251
-        it 'can reorder the events_to_show last events based on a Liquid expression' do
291
+        it 'can reorder the last `events_to_show` events based on a Liquid expression' do
252 292
           agent.options['events_to_show'] = 2
253 293
           asc_content, _status, _content_type = agent.receive_web_request({ 'secret' => 'secret2' }, 'get', 'application/json')
254 294
           expect(asc_content['items'].map {|i| i["title"] }).to eq(["Evolving", "Evolving again"])